<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;
use App\Role;
use Validator;
use App\Http\Requests\StoreUserPost;

class AdminUserController extends Controller
{
    //
    protected function index(){
    	$list = Admin::get();
    	return view('admin/adminuser/index',['list'=>$list]);
    }


	//添加用户
	protected function add(){
		if(request()->isMethod('post')){
			$input = request()->only(['username','password','confirm_password','mobile','status','role_id']);	
			$user = new Admin;
			$res = $user->check($input);
			if($res){
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> $res,
					'url'	=>''
					));					
			}
			$user->username			= $input['username'];
			$user->password			= md5($input['password']);
			$user->mobile			= $input['mobile'];
			$user->status			= $input['status'];
			$user->last_login_time	= date("Y-m-d H:i:s");
			$user->last_login_ip	= date("Y-m-d H:i:s");
			$user->save();
			$user->roles()->attach($input['role_id']); //只需传递id即可
			return response()->json(array(
				'code'	=> 1,
				'msg'	=> '添加成功',
				'url'	=>route('admin.adminuser.index')
				));	
		}
		
		$role = Role::get();
		return view('admin/adminuser/add',['data'=>$role]);
	}


	//删除
	protected function del(){
		$id = request('id');
		$user = Admin::find($id);
		if($user->delete()){
			return response()->json(array(
				'code'	=> 1,
				'msg'	=> '删除成功',
				'url'	=>''
				));					
		}else{
			return response()->json(array(
				'code'	=> 0,
				'msg'	=> '删除失败',
				'url'	=>''
				));				
		}
	}
	
	//编辑
	protected function edit(){
		if(request()->isMethod('post')){
			$input = request()->only(['id','username','password','confirm_password','mobile','status','role_id']);
	        $validator = Validator::make($input, [
	        	'id' =>	'required',
	            'username' => 'required',
	            'password' => 'required|same:confirm_password',
	            'confirm_password' => 'required',
	        ]);			
	        if ($validator->fails()) {
	            $errors = $validator->errors();
				foreach ($errors->all() as $message) {
					return response()->json(array(
						'code'	=> 0,
						'msg'	=> $message,
						'url'	=>''
						));	
				}
	        }				
			$user = Admin::find($input['id']);
			$user->username			= $input['username'];
			$user->password			= md5($input['password']);
			$user->mobile			= $input['mobile'];
			$user->status			= $input['status'];
			if($user->save()){
				$user->roles()->sync([$input['role_id']]); //设置用户角色
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '编辑成功',
					'url'	=>route('admin.adminuser.index')
					));					
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '编辑失败',
					'url'	=> '',
					));
			}		
		}
		
		$id = request('id');
		$user = Admin::find($id);
		$res = $user->roles()->first();
		$role = Role::get();
		return view('admin/adminuser/edit',['user'=>$user,'role'=>$role,'user_role'=>$res]);
	}
}
